using System;

namespace SharpMud.Server.Systems
{
	/// <summary>
	/// This server system, when implemented by an inheriting class, has a its work
	/// down in a centralized function that must be called repeatedly.
	/// In this implementation of the 'pulsed' server system, the pulsing is handled
	/// using advanced control of the current thread.
	/// </summary>
	public class PulsarServerSystem : ThreadedServerSystem
	{
		protected override void WorkThreadStart()
		{
			PrePulseLoop();
			do
			{
				DoPulseLoopWait();
				Pulse();
			} while(this.ThreadShouldBeAlive);
			PostPulseLoop();
		}

		private void DoPulseLoopWait()
		{
			System.Threading.Thread.Sleep(GetPulseLoopInterval());
		}

		protected virtual void PrePulseLoop()	{}
		protected virtual void Pulse()			{}
		protected virtual void PostPulseLoop()  {}
		
		protected virtual System.TimeSpan GetPulseLoopInterval()
		{
			return System.TimeSpan.FromSeconds(0.05);
		}
	}
}
